<?php
namespace app\common\model;

use think\Model;

class NewhouseRealscene extends Model
{
    // 表名
    protected $name = 'newhouse_realscene';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'category_text',
        'classify_text',
        'flag_text'
    ];

    protected static function init()
    {
        self::afterInsert(function ($row) {
            self::afterAction($row);
        });
        self::afterUpdate(function ($row) {
            self::afterAction($row);
        });

    }

    /**
     * 之后动作
     * @param array $row
     */
    protected static function afterAction($row)
    {
        $pk = $row->getPk();
        switch ( $row->getAttr('category') ) {
            case '1':
                $res = model('newhouseLayout')->get( $row->getAttr('layout_id') );
                $row->getQuery()->where($pk, $row[$pk])->update(['title' => $res->title]);
                break;
            case '3':
                $row->getQuery()->where($pk, $row[$pk])->update(['title' => $row->classify_text]);
                break;
        }
    }

    public function testTitle()
    {
        return 'TestTitle';
    }


    public function getCategoryList()
    {
        return ['1' => __('Category 1'), '2' => __('Category 2'), '3' => __('Category 3'), '4' => __('Category 4'), '6' => __('Category 6')];
    }

    public function getClassifyList()
    {
        return ['0' => __('Classify 0'), '1' => __('Classify 1'), '2' => __('Classify 2'), '3' => __('Classify 3')];
    }

    public function getFlagList()
    {
        return ['0' => __('Flag 0'), '1' => __('Flag 1')];
    }


    public function getCategoryTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['category']) ? $data['category'] : '');
        $list = $this->getCategoryList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getClassifyTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['classify']) ? $data['classify'] : '');
        $list = $this->getClassifyList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getFlagTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['flag']) ? $data['flag'] : '');
        $list = $this->getFlagList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function admin()
    {
        return $this->belongsTo('\app\admin\model\Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function newhouse()
    {
        return $this->belongsTo('Newhouse', 'newhouse_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
